using System;
using System.Collections.Generic;
using System.Text;

namespace SilverlightBlowfish
{
    class BlowfishCBC : BlowfishECB
    {
        private long m_lCBCIV;

        public BlowfishCBC(byte[] bfkey)
            : base(bfkey)
        {
            SetCBCIV(0L);
        }
        public BlowfishCBC(byte[] bfkey, long lInitCBCIV)
            : base(bfkey)
        {
            SetCBCIV(lInitCBCIV);
        }
        public BlowfishCBC(byte[] bfkey, byte[] initCBCIV)
            : base(bfkey)
        {
            SetCBCIV(initCBCIV);
        }

        public new void CleanUp()
        {
            m_lCBCIV = 0L;
            base.CleanUp();
        }

        public long GetCBCIV()
        {
            return m_lCBCIV;
        }
        public void SetCBCIV(long lNewCBCIV)
        {
            m_lCBCIV = lNewCBCIV;
        }

        public void GetCBCIV(byte[] dest)
        {
            PublicFuntion.LongToByteArray(m_lCBCIV, dest, 0);
        }
        public void SetCBCIV(byte[] newCBCIV)
        {
            m_lCBCIV = PublicFuntion.ByteArrayToLong(newCBCIV, 0);
        }

        private long EncryptBlockCBC(long lPlainblock)
        {
            lPlainblock ^= m_lCBCIV;
            lPlainblock = base.EncryptBlock(lPlainblock);
            return m_lCBCIV = lPlainblock;
        }
        private long DecryptBlockCBC(long lCipherblock)
        {
            long lTemp = lCipherblock;
            lCipherblock = base.DecryptBlock(lCipherblock);
            lCipherblock ^= m_lCBCIV;
            m_lCBCIV = lTemp;
            return lCipherblock;
        }
    }
}
